//
//  RTJHomeCell.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/11/28.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJHomeCell.h"
#import "ZXYGradientProgress.h"
#import "RTJAutoMarkCollectionCell.h"

@interface RTJHomeCell ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    
    /** 渐变色圆弧进度 */
    ZXYGradientProgress *_gradientProgress;
}



@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@end

@implementation RTJHomeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self backview:_bg_view viewOfcornerRadius:_bg_view.height/2];
    [self backview:_bg_view1 viewOfcornerRadius:5];
    [self backview:_bg_view2 viewOfcornerRadius:5];
    [self backview:_bg_view3 viewOfcornerRadius:5];
    [self backview:_bg_view4 viewOfcornerRadius:5];
    
    _tagView.layer.cornerRadius = _tagView.height/2;
    _tagView.layer.masksToBounds = YES;
    _tagView1.layer.cornerRadius = _tagView1.height/2;
    _tagView1.layer.masksToBounds = YES;

    _joinBtn.layer.cornerRadius = _joinBtn.height/2;
    _joinBtn.layer.masksToBounds = YES;
    _joinBtn.layer.borderColor = [UIColor colorForHex:@"#FFBC04"].CGColor;
    _joinBtn.layer.borderWidth = 1;
    
//    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(2, 1, self.round_view.width+3, self.round_view.width+3)];
//    img.image = [UIImage imageNamed:@"yinying"];
//    [self.round_view addSubview:img];
    
    _gradientProgress = [[ZXYGradientProgress alloc] initWithFrame:CGRectMake(0, 0, self.round_view.width-5, self.round_view.width-5) progress:0.8];
    _gradientProgress.bottomColor = [UIColor colorForHex:@"#FFE3E3"];
    [self.round_view addSubview:_gradientProgress];
    
    
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //     layout.itemSize = CGSizeMake(215,225);
    //    layout.minimumLineSpacing = 1;//设置最小行间距
    layout.minimumInteritemSpacing = 2;//item间距(最小值)
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);//设置section的编距
    
    self.collectionView.collectionViewLayout=layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.backgroundView=[[UIView alloc] initWithFrame:CGRectZero];
    self.backgroundView.backgroundColor = [UIColor whiteColor];
        
}

-(void)setHomedata:(NSDictionary *)homedata{
    
    if (_homedata!= homedata) {
        _homedata = homedata;
    }
    
    //
    
    if ([_homedata[@"new_deal"] isKindOfClass:[ NSDictionary class]]) {

        //新人
        NSString *max_rate = _homedata[@"new_deal"][@"rate"];
        NSArray *arr1 = [max_rate componentsSeparatedByString:@"."]; //字符串分隔成数组
        
        NSString *str = [NSString stringWithFormat:@"%@",arr1[0]];
        
        //    _max_rate.text = [NSString stringWithFormat:@"%@",arr1[0]];
        //  _min_rate.text = @".00%+3.0%";
        
        NSInteger user_bid_rebate = [_homedata[@"new_deal"][@"user_bid_rebate"] integerValue];
        
        if (user_bid_rebate == 0) {
            
            if (str.length == 2) {
                
                str = [NSString stringWithFormat:@"%@%@",str,@"%"];
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:2];
                
                
                
            }else{
                
                str = [NSString stringWithFormat:@"%@%@",str,@"%"];
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:1];
                
            }
            
        }else{
            
            if (str.length == 2) {
                
                str = [NSString stringWithFormat:@"%@%@+%ld%@",str,@"%",(long)user_bid_rebate,@"%"];
                
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:1];
                
            }else{
                
                str = [NSString stringWithFormat:@"%@%@+%ld%@",str,@"%",(long)user_bid_rebate,@"%"];
                
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:1];
                
            }
            
        }
        
        _term.text = [NSString stringWithFormat:@"%@",_homedata[@"new_deal"][@"repay_time"]];
        _xinren_max.text = [NSString stringWithFormat:@"借款总额：%@元",_homedata[@"new_deal"][@"borrow_amount_format"]];
        _xinren_min.text = [NSString stringWithFormat:@"起投资金：%@元",_homedata[@"new_deal"][@"min_loan_money_format"]];
        
        
    }
   
    
   _autotipLab.text = _homedata[@"plan_list"][0][@"name"];
   _tips.text = [NSString stringWithFormat:@"%@元起投",_homedata[@"plan_list"][0][@"min_load_money"]];
   _aterm.text = [NSString stringWithFormat:@"%@",_homedata[@"plan_list"][0][@"repay_time"]];

    NSArray *plan_list = _homedata[@"plan_list"];
    
    if (plan_list.count !=0) {
        
        NSString  *ratestr = [NSString stringWithFormat:@"%@",_homedata[@"plan_list"][0][@"rate"]];
        
            if (ratestr.length == 2) {
                
                ratestr = [NSString stringWithFormat:@"%@%@",ratestr,@"%"];
                _amax_rate.attributedText = [self getSubStrNumFromStr:ratestr WithLocation:2];
                
            }else{
                
                ratestr = [NSString stringWithFormat:@"%@%@",ratestr,@"%"];
                _amax_rate.attributedText = [self getSubStrNumFromStr:ratestr WithLocation:1];
                
            }
        }
   
    //自动标
//    NSString *amax_rate = _homedata[@"auto_bid"][@"rate"][@"max_rate"];
//    NSString *amin_rate = _homedata[@"auto_bid"][@"rate"][@"min_rate"];
//    NSArray *arr3 = [amax_rate componentsSeparatedByString:@"."]; //字符串分隔成数组
//    NSArray *arr4 = [amin_rate componentsSeparatedByString:@"."]; //字符串分隔成数组
    
//    _amax_rate.text = [NSString stringWithFormat:@"%@~%@",arr4[0],arr3[0]];
//    _amin_rate.text = @"%";
//
//   ÷ _tips.text = [NSString stringWithFormat:@"100元起投 %@人已加入",_homedata[@"auto_bid"][@"count"]];
//    _aterm.text = [NSString stringWithFormat:@"%@~%@",_homedata[@"auto_bid"][@"term"][@"min"],_homedata[@"auto_bid"][@"term"][@"max"]];
    
    _anquanLab.text = [NSString stringWithFormat:@"%@",_homedata[@"days"]];
    _zhuceLab.text = [NSString stringWithFormat:@"%@",_homedata[@"register_num"]];
    _chengjiaoLab.text = [NSString stringWithFormat:@"%@",_homedata[@"all_money"]];
    _daishouLab.text = [NSString stringWithFormat:@"%@",_homedata[@"daishou_amount"]];
    
    [self.collectionView reloadData];
    
}

- (NSMutableAttributedString *)getSubStrNumFromStr:(NSString *)str WithLocation:(NSInteger) index{

    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:str];
    
    [AttributedStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Medium" size:25] range:NSMakeRange(0, index)];
    
    [AttributedStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Regular" size:12] range:NSMakeRange(index, str.length-index)];
    
    return AttributedStr;

}

-(void)setAutomarkData:(NSDictionary *)automarkData{
    
    //精选服务
    _headerTitle.text = @"精选服务";
    _markTitle.text = [NSString stringWithFormat:@"%@",automarkData[@"name"]];
    _typelabs.text = [NSString stringWithFormat:@"%@",automarkData[@"loantype_format"]];
    
    NSString *str3 =[NSString stringWithFormat:@"%@",automarkData[@"rate"]];
    NSArray *arr3 = [str3 componentsSeparatedByString:@"."]; //字符串分隔成数组
    _numLab.text = arr3[0];
    _small.text = @"%";
    _time.text = [NSString stringWithFormat:@"%@",automarkData[@"repay_time"]];
    NSString *str4 = [NSString stringWithFormat:@"%@",automarkData[@"percent"]];
    _gradientProgress.progress = [str4 floatValue]/100;
    _broowMoney.text = [NSString stringWithFormat:@"%@",automarkData[@"borrow_amount"]];
    
    NSInteger repay_time_type = [automarkData[@"repay_time_type"] integerValue];
    
    if (repay_time_type == 0) {
        _time_type.text = @"天";
    }else if (repay_time_type == 1){
        _time_type.text = @"个月";
    }else{
        _time_type.text = @"年";
    }
    
    NSInteger is_new = [automarkData[@"is_new"]integerValue];
    
    NSString *encryption = [NSString stringWithFormat:@"%@",automarkData[@"encryption"]];
    
    if (is_new == 0) {
        
        _tagLab.text =  encryption.length!=0 ? @"约标" : @"散标";
        _tagLab1.hidden =  YES;
        _tagView1.hidden =  YES;
        
    }else{
        
        _tagLab.text = @"新手专享";
        _tagLab1.hidden = encryption.length!=0 ? NO : YES;
        _tagView1.hidden = encryption.length!=0 ? NO : YES;
        _tagLab1.text =  encryption.length!=0 ? @"约标" : @"散标";
        
    }
    
}

-(void)setLenddata:(NSDictionary *)lenddata{
    
    _viewHeight.constant = 0;
    
    if ([lenddata[@"new_deal"] isKindOfClass:[ NSDictionary class]]) {
    
        //新人
        NSString *max_rate = lenddata[@"new_deal"][@"rate"];
        NSArray *arr1 = [max_rate componentsSeparatedByString:@"."]; //字符串分隔成数组
        
        NSString *str = [NSString stringWithFormat:@"%@",arr1[0]];
        
        //    _max_rate.text = [NSString stringWithFormat:@"%@",arr1[0]];
        //  _min_rate.text = @".00%+3.0%";
        
        NSInteger user_bid_rebate = [_homedata[@"new_deal"][@"user_bid_rebate"] integerValue];
        
        if (user_bid_rebate == 0) {
            
            if (str.length == 2) {
                
                str = [NSString stringWithFormat:@"%@%@",str,@"%"];
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:2];
                
                
                
            }else{
                
                str = [NSString stringWithFormat:@"%@%@",str,@"%"];
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:1];
                
            }
            
        }else{
            
            if (str.length == 2) {
                
                str = [NSString stringWithFormat:@"%@%@+%ld%@",str,@"%",(long)user_bid_rebate,@"%"];
                
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:1];
                
            }else{
                
                str = [NSString stringWithFormat:@"%@%@+%ld%@",str,@"%",(long)user_bid_rebate,@"%"];
                
                _max_rate.attributedText = [self getSubStrNumFromStr:str WithLocation:1];
                
            }
            
        }
    
        _term.text = [NSString stringWithFormat:@"%@",lenddata[@"new_deal"][@"repay_time"]];
        _xinren_max.text = [NSString stringWithFormat:@"借款总额：%@元",lenddata[@"new_deal"][@"borrow_amount_format"]];
        _xinren_min.text = [NSString stringWithFormat:@"起投资金：%@元",lenddata[@"new_deal"][@"min_loan_money_format"]];
    }
    
//    _max_rate.text = [NSString stringWithFormat:@"%@",arr1[0]];
//
//    NSInteger user_bid_rebate = [lenddata[@"new_deal"][@"user_bid_rebate"] integerValue];
//
//    if (user_bid_rebate == 0) {
//
//        _min_rate.text = @"%";
//
//    }else{
//
//        _min_rate.text = [NSString stringWithFormat:@"%@+%ld%@",@"%",(long)user_bid_rebate,@"%"];
//    }

    
   //自动标
//    NSString *amax_rate = lenddata[@"auto_bid"][@"rate"][@"max_rate"];
//    NSString *amin_rate = lenddata[@"auto_bid"][@"rate"][@"min_rate"];
//    NSArray *arr3 = [amax_rate componentsSeparatedByString:@"."]; //字符串分隔成数组
//    NSArray *arr4 = [amin_rate componentsSeparatedByString:@"."]; //字符串分隔成数组
//
//    _amax_rate.text = [NSString stringWithFormat:@"%@~%@",arr4[0],arr3[0]];
//    _amin_rate.text = @"%";
//
//    _tips.text = [NSString stringWithFormat:@"100元起投 %@人已加入",lenddata[@"auto_bid"][@"count"]];
//    _aterm.text = [NSString stringWithFormat:@"%@~%@",lenddata[@"auto_bid"][@"term"][@"min"],lenddata[@"auto_bid"][@"term"][@"max"]];
    
    
}

-(void)setAutodata:(NSDictionary *)autodata{
    
    //    //自动标
    _autoTitle.text = autodata[@"name"];
    NSString *amax_rate = [NSString stringWithFormat:@"%@",autodata[@"rate"]];
    _autoNum.text = [NSString stringWithFormat:@"%@",amax_rate];

    NSString *increase_rate = [NSString stringWithFormat:@"%@",autodata[@"increase_rate"]];
    
    if ([increase_rate isEqualToString:@"0.00"]) {
       
        _autoSmall.text = @"%";

    }else{
        
        _autoSmall.text = [NSString stringWithFormat:@"+%@%@",increase_rate,@"%"];

    }
//    _autoType.text = [NSString stringWithFormat:@"%@",autodata[@"loantype_format"]];
    _autoType.text = @"";

    _autoTime.text = [NSString stringWithFormat:@"%@",autodata[@"repay_time"]];
    
}

-(void)setMarkData:(NSDictionary *)markData{
    
    _headerTitle.text = @"优质项目";
    _markTitle.text = [NSString stringWithFormat:@"%@",markData[@"name"]];
    _typelabs.text = [NSString stringWithFormat:@"%@",markData[@"loantype_format"]];
    
    NSString *str3 =[NSString stringWithFormat:@"%@",markData[@"rate"]];
    NSArray *arr3 = [str3 componentsSeparatedByString:@"."]; //字符串分隔成数组
    _numLab.text = arr3[0];
    _small.text = @"%";
    _time.text = [NSString stringWithFormat:@"%@",markData[@"repay_time"]];
    NSString *str4 = [NSString stringWithFormat:@"%@",markData[@"percent"]];
    _gradientProgress.progress = [str4 floatValue]/100;
    _broowMoney.text = [NSString stringWithFormat:@"%@",markData[@"borrow_amount"]];
    
    NSInteger is_new = [markData[@"is_new"]integerValue];
    
    NSString *encryption = [NSString stringWithFormat:@"%@",markData[@"encryption"]];
    
    if (is_new == 0) {
        
        _tagLab.text =  encryption.length!=0 ? @"约标" : @"散标";
        _tagLab1.hidden =  YES;
        _tagView1.hidden =  YES;
        
    }else{
        
        _tagLab.text = @"新手专享";
        _tagLab1.hidden = encryption.length!=0 ? NO : YES;
        _tagView1.hidden = encryption.length!=0 ? NO : YES;
        _tagLab1.text =  encryption.length!=0 ? @"约标" : @"散标";
        
    }
    
}


-(void)backview:(UIView *)backview viewOfcornerRadius:(NSInteger)cornerRadius{
    
    backview.layer.cornerRadius = cornerRadius;
    backview.layer.shadowColor = [UIColor colorForHex:@"#dbc9cc"].CGColor;
    backview.layer.shadowOffset = CGSizeMake(-1, 1);
    backview.layer.shadowOpacity = 0.5;
    backview.layer.shadowRadius = 5;
    
}


#pragma makr -UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [_homedata[@"plan_list"] count];
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //在这里注册自定义的XIBcell否则会提示找不到标示符指定的cell
    UINib *nib = [UINib nibWithNibName:@"RTJAutoMarkCollectionCell"bundle: [NSBundle mainBundle]];
    
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"RTJAutoMarkCollectionCell"];
    
    RTJAutoMarkCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RTJAutoMarkCollectionCell" forIndexPath:indexPath];
    cell.titleLab.text = _homedata[@"plan_list"][indexPath.row][@"name"];
    cell.rateLab.text = [NSString stringWithFormat:@"%@", _homedata[@"plan_list"][indexPath.row][@"rate"]];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *markId = [NSString stringWithFormat:@"%@", _homedata[@"plan_list"][indexPath.row][@"id"]];
    
    if (self.delegate&&[self.delegate respondsToSelector:@selector(autoMarkId:)]) {
    
        [self.delegate autoMarkId:markId];
    }
    
}

#pragma mark - UICollectionViewDelegateFlowLayout
// 设置格子的大小
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
   sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(140,153);
    
}


// 设置两行之间cell间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}



// 设置同行之间cell间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
